package com.viknix.threadpool.manager.autoconfigure;

import com.viknix.threadpool.manager.client.alertor.AlertorHandler;
import com.viknix.threadpool.manager.client.message.DingSendMessageHandler;
import com.viknix.threadpool.manager.client.message.SendMessageHandler;
import com.viknix.threadpool.manager.client.message.SendMessageService;
import com.viknix.threadpool.manager.client.message.SendMessageServiceImpl;
import com.viknix.threadpool.manager.client.model.InstanceInfo;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Optional;

/**
 * @Author: Dongqi
 * @Date: 2021/10/22 13:39
 * @Version 1.0
 * @Description: 告警配置类
 */
@AllArgsConstructor
public class TpmAlertConfig {

    private InstanceInfo instanceInfo;
    private TpmConfigProperties properties;
    private ConfigurableEnvironment environment;

    /**
     * 消息发生器
     *
     * @return
     */
    @Bean
    @DependsOn("applicationContextHolder")
    public SendMessageService sendMessageService() {
        return new SendMessageServiceImpl(properties.getMessageReceivers());
    }

    /**
     * 钉钉告警
     *
     * @return
     */
    @Bean
    public SendMessageHandler dingSendMessageHandler() {
        String active = environment.getProperty("spring.profiles.active", Strings.EMPTY);
        Long period = Optional.ofNullable(properties.getPeriod()).orElse(5L);
        return new DingSendMessageHandler(instanceInfo, active, period);
    }

    @Bean
    public AlertorHandler alertorHandler() {
        Long period = properties.getPeriod();
        return new AlertorHandler(period);
    }
}
